package com.example.springcloud.controller;

import com.example.springcloud.entities.CommonResult;
import com.example.springcloud.entities.Payment;
import com.example.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-01-28
 * Time: 15:28
 */

@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;


    @PostMapping("/create")
    public CommonResult crete(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("create result:{}", result);

        if (result > 0) {
            return new CommonResult(200, "插入数据库成功！,serverPort:" + serverPort, result);
        }
        return new CommonResult(500, "插入数据库失败！");
    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);

        log.info("getPaymentById:{}", payment);

        if (payment != null) {
            return new CommonResult<>(200, "查询成功！,serverPort:" + serverPort, payment);
        }

        return new CommonResult<>(400, "查询失败！,serverPort:" + serverPort);
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }

}
